import java.util.ArrayList;
import java.util.Scanner;
public class Run1 {
    public  static  fileInUse[] fileList = new fileInUse[6];//初始化空白文件目录
    protected  static  int nowEmptyFile = 100;//初始化空闲区
    protected  static  void addFile(){
        int i;
        for (i=0;i<3;i++) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入文件名");
            String fileName = sc.nextLine();
            System.out.println("请输入申请块号");
            int M = sc.nextInt();
            System.out.println("请输入首空白号");
            int firstPiece = sc.nextInt();
            if (M>nowEmptyFile) return;
            fileList[i] = new fileInUse();
            fileList[i].fileName = fileName;
            fileList[i].firstPiece = firstPiece;
            fileList[i].emptyNums = M;
            nowEmptyFile = nowEmptyFile - M;
        }
        System.out.println("----------当前已用的文件目录--------------");
        for (i=0;i<fileList.length;i++){
            if (i == 3) release();//转去执行释放操作
            System.out.println("文件名 首块号 空白块个数 ");
            System.out.println(fileList[i].getFileName() + "\t\t" + fileList[i].getFirstPiece() + "\t\t" + fileList[i].getEmptyNums());
            for (int j = 0; j < fileList[i].getEmptyNums(); j++) {
                int firsPiece = fileList[i].getFirstPiece();
                System.out.println("物理块号：" + (firsPiece + j));
            }
        }
    }
    public  static  void release(){
        int i,j;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的首块号");int F = sc.nextInt();
        System.out.println("请输入要删除的文件块数");int B = sc.nextInt();
        for ( j=1;j<2;j++){
            for (i=0;i<fileList.length;i++){//遍历空白文件目录表
                if (F+B == j){//有下邻
                    fileList[j].emptyNums += B;
                    fileList[j].firstPiece = F;
                }else if(F == fileList[j].getFirstPiece() + fileList[j].getEmptyNums()){//有上邻
                    if(fileList[j].firstPiece == F+B){//有上邻也有下邻；
                        fileList[j].emptyNums = fileList[j+1].emptyNums + B;
                        fileList[j+1].emptyNums = 0;
                        fileList[j+1].firstPiece = 0;//清空j+1栏
                    }
                }else{
                    fileList[j].emptyNums += B;
                }
                System.out.println("-----------当前空闲文件目录-------------");
                System.out.println("序号  首块号  空白文件块");
                for (i = 0;i<fileList.length-1;i++){
                    System.out.println(i + "\t\t" + fileList[i].getFirstPiece() +"\t\t" +
                            (nowEmptyFile-fileList[i].getEmptyNums()));
                    if (i == 2) break;
                }
                System.out.println("----------当前已用的文件目录--------------");
                for (i=0;i<fileList.length;i++){
                    System.out.println("文件名 首块号 空白块个数 ");
                    System.out.println(fileList[i].getFileName() + "\t\t" + fileList[i].getFirstPiece() + "\t\t" + fileList[i].getEmptyNums());
                    for (j = 0; j < fileList[i].getEmptyNums(); j++) {
                        int firsPiece = fileList[i].getFirstPiece();
                        System.out.println("物理块号：" + (firsPiece + j));
                    }
                }
            }
        }
    }
    public static void main(String[] args) {
        addFile();
    }
}

class fileInUse {
    protected  String fileName;//文件名
    protected  int firstPiece;//首块号
    protected  int emptyNums;//空白块个数
    
    public String getFileName() {
        return fileName;
    }
    
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    
    public int getFirstPiece() {
        return firstPiece;
    }
    
    public void setFirstPiece(int firstPiece) {
        this.firstPiece = firstPiece;
    }
    
    public int getEmptyNums() {
        return emptyNums;
    }
    
    public void setEmptyNums(int emptyNums) {
        this.emptyNums = emptyNums;
    }
}